<?php
/* 
 * 后台报备管理
 */
class ReportController extends AdminController
{
    private $pagesize = 20;
    /**
     * 报备列表
     * 上级可以审核下级
     */
    public function actionIndex()
    {
        $p = $this->get('p', 1);
        $project_id = (int)$this->get('project_id');

        $netreport = new NetworkReport();
        $criteria = new CDbCriteria();
        if ($project_id) 
        {
            $criteria->addColumnCondition(array('project_id' => $project_id));
        }
        $criteria->addCondition('status!=0');
        $total = $netreport->count($criteria);
        
        $criteria->order = 'create_time desc';
        $criteria->limit = $this->pagesize;
        $criteria->offset = ($p - 1) * $this->pagesize;
        $criteria->order = 'create_time desc';
        $list = $netreport->findAll($criteria);
        if ($list) {
            foreach ($list as $key => $val) {
                $list[$key]['network_info'] = NetWork::model()->findByPk($val['network_id']);
                $list[$key]['project_info'] = Project::model()->findByPk($val['project_id']);
            }
        }
        $page = new Page($total, $this->pagesize);
        //所有项目
        $project_list = Project::model()->findAll('status='.Project::PROJECT_RUNNING);
        $this->smarty->assign('project_list', $project_list);
        $this->smarty->assign('list', $list);
        $this->smarty->assign('project_id', $project_id);
        $this->smarty->assign('page', $page->style1());
        $this->smarty->assign('audit', NetworkReport::$audit);
        $this->smarty->assign('status', NetworkReport::$status);
        $this->smarty->display('report/list.tpl'); 
    }

    /**
     * 历史记录
     */
    public function actionHistory()
    {
        $nr_id = $this->get('nr_id');
        $criteria = new CDbCriteria();
        $criteria->addColumnCondition(array('nr_id' => $nr_id));
        $criteria->order = 'create_time desc';
        $netaudit = new NetworkReportAudit();
        $list = $netaudit->findAll($criteria);
        if ($list) {
            foreach ($list as $key => $val) {
                $info = NetWork::model()->findByPk($val['network_id'])->attributes;
                $list[$key]['network_name'] = $info['network_name'];
            }
        }
        $this->smarty->assign('level', NetWork::networkLevel());
        $this->smarty->assign('status', NetworkReportAudit::$status);
        $this->smarty->assign('list', $list);
        $this->smarty->display('report/history.tpl');
    }
    /**
     * 附件列表
     */
    public function actionAttachment()
    {
        $nr_id = $this->get('nr_id');
        $list = NetworkReportAttach::model()->findAll('nr_id='.$nr_id);
        $this->smarty->assign('list', $list);
        $this->smarty->display('report/attach.tpl');
    }
    /**
     * 删除附件
     */
    public function actionDelAttach()
    {
        $nra_id = (int)$this->get('nra_id');
        $info = NetworkReportAttach::model()->findByPk($nra_id);
        $info->status = 0;
        if ($info->update()) {
            //修改是否有附件
            if (NetworkReportAttach::model()->count('network_id='.$info->network_id.' and nr_id='.$info->nr_id.' and status=1')==0) {
                NetworkReport::model()->updateByPk($info->nr_id,array('haveattach' => 0));
            }
            $this->_ajax(array('status' => 1,'note' => '删除成功'));
        } else {
            $this->_ajax(array('status' => 0,'note' => '删除失败'));
        }
    }
    /**
     * 删除报备信息，批量删除
     * 
     */
    public function actionDel()
    {
        $nr_id = $this->request('nr_id');
        if (empty($nr_id)) {
            $this->_ajax(array('status' => 0,'note' => '请选择要删除的数据'));
        }
        if (NetworkReport::model()->updateByPk($nr_id, array('status' => 0))){
            //同时要删除对应的审核内容
            if (is_array($nr_id)) {
                NetworkReportAudit::model()->updateAll(array('status'=>1,'nr_id in('.join(',', $nr_id).')'));
            } else {
                NetworkReportAudit::model()->updateAll(array('status'=>1,'nr_id=:nr_id',array(':nr_id' => $nr_id)));
            }
            
            $this->_ajax(array('status' => 1,'note' => '删除成功'));
        } else {
            $this->_ajax(array('status' => 0,'note' => '删除失败'));
        }
    }
    /**
     * 根据项目删除
     */
    public function actionDelProject()
    {
        $project_id = (int)$this->get('project_id');
        if (!$project_id) {
            $this->jump('请先选择要删除的项目');
        }
        if (NetworkReport::model()->updateAll(array('status' => 0), 'project_id=:project_id',array(':project_id' => $project_id))) {
            $this->jump('删除成功','/webadmin/report');
        } else {
            $this->jump('删除失败');
        }
    }
	
	/**
     * 根据项目导出
     */
    public function actionExportProject()
    {
        $project_id = (int)$this->get('project_id');
		if (!$project_id) {
            $this->jump('请先选择要导出的项目');
        }
        $netreport = new NetworkReport();
        $criteria = new CDbCriteria();
        if ($project_id) 
        {
            $criteria->addColumnCondition(array('project_id' => $project_id));
        }
        $criteria->addCondition('status!=0');
		$list = $netreport->findAll($criteria);
		if (!$list) {
			$this->jump('当前项目无报备信息');
		}
		
		# 项目 & 网点信息
		$items = array();
		$excelData = array();
		$project = Project::model()->find('project_id='.$project_id)->attributes;
		$audit = NetworkReport::$audit;
		$status = NetworkReport::$status;
		foreach ($list as $value) {
			$network = NetWork::model()->find('network_id='.$value['network_id'])->attributes;
			$reportAudit = NetworkReportAudit::model()->findAll('nr_id='.$value['nr_id']);
			
			# 组件数据
			$excelData[$value['nr_id']] = array(
				'network_num' => $network['network_num'],
				'network_name' => $network['network_name'],
                                'nr_order' => $value['nr_order'],
				'project_name' => $project['project_name'],
				'number' => $value['nr_id'],
				'content' => $value['content'],
				'start_time' => date('Y-m-d H:i:s', $value['start_time']),
				'end_time' => date('Y-m-d H:i:s', $value['end_time']),
				'self_status' => $audit[$value['audit']] . $status[$value['status']],
			);
			
			foreach ($reportAudit as $auditVal) {
				$aLevel = $auditVal['level'];
				switch ($aLevel) {
					case 1:
						$excelData[$value['nr_id']]['one_status'] = $audit[$aLevel] . $status[$auditVal['status']];
						break;
					case 2:
						$excelData[$value['nr_id']]['two_status'] = $audit[$aLevel] . $status[$auditVal['status']];
						break;
					case 3:
						$excelData[$value['nr_id']]['three_status'] = $audit[$aLevel] . $status[$auditVal['status']];
						break;
					default:
						break;
				}
			}
			$excelData[$value['nr_id']]['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
		}
		
		$this->enablePHPExcel();
		$networkExcel = new NetworkExcel();
		$networkExcel->reportExport($project_id, $excelData, '问卷报备导出_项目ID' . $project_id);
    }
	
}

